﻿using FluentScheduler;

namespace Bigway.Common.Jobs;

public class BaseJobRegistry: Registry
{
    private readonly Dictionary<string, BackgroundJobInfo> _jobMaps = [];

    public BackgroundJobInfo? GetJobInfo(string jobId)
        => _jobMaps.GetValueOrDefault(jobId);

    public BackgroundJobInfo? GetJobInfo<TJob>(string systemId) 
        => GetJobInfo(systemId, typeof(TJob).Name);

    public BackgroundJobInfo? GetJobInfo(string systemId, string jobName)
    {
        var jobId = IBackgroundJob.GetJobId(systemId, jobName);
        return GetJobInfo(jobId);
    }

    protected void ScheduleBackgroundJob(IBackgroundJob job, Action<Schedule> scheduler)
    {
        var schedule = Schedule(job);
        scheduler(schedule.WithName(job.JobId));

        _jobMaps[job.JobId] = new BackgroundJobInfo
        {
            Job = job,
            Schedule = schedule
        };
    }
}